package com.jqk.shop_product.service.impl;

import com.jqk.shop_product.bean.Product;
import com.jqk.shop_product.dao.ProductDao;
import com.jqk.shop_product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @Author:jiangqikun
 * @Date:2022/3/1 14:46
 **/

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductDao productDao;

    /**
     * 通过产品 id 查找产品信息
     */
    @Override
    public Product findByPid(Integer pid) {
        return productDao.findById(pid).orElse(null);
    }

    /**
     * 扣减库存
     */
    @Override
    public void reduceProduct(Integer pid, int num) {
        Product product=findByPid(pid);
        if (product.getStock() < num) {
            throw new RuntimeException("库存不足");
        }
        int i = 1 / 0;
        product.setStock(product.getStock() - num);
        productDao.save(product);
    }

}